﻿<%@ WebHandler Language="C#" Class="GetListkeywordsnews" %>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;
using System.Text;
using System.Collections;
using System.Net;
using System.Xml;
using BusinessLogic.ClassFactory;
using LitJson;
using RichECMS;

public class GetListkeywordsnews : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
        Hashtable result = new Hashtable();
        string thisaid = HttpContext.Current.Request["aid"];
        string sctxt = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["keys"]);

        StringBuilder sbWhere = new StringBuilder();
            if (sctxt.Trim() != ""&&sctxt.Trim()!=",,,")
            {
			    string[] keylist=sctxt.Trim().Split(',');
				
				for(int i=0;i<keylist.Length;i++)
				{
					if(keylist[i].Trim()!="")
					{
						if(i==0)
						{
							sbWhere.AppendFormat(" and ( chvKeyWords like '%{0}%'", keylist[i]);
						}
						else
						{
							sbWhere.AppendFormat(" or chvKeyWords like '%{0}%' ", keylist[i]);
						}
					}
				}
                if(sbWhere.ToString()!="")
                {
                    sbWhere.Append(" ) ");
                }
            }
			else
			{
				sbWhere.Append(" and 2=3 ");
			}
            sbWhere.Append(" and 2=2 ");
            if (IsNum(thisaid))
            {
                sbWhere.Append(" and intArticleId!=" + thisaid);
            }

        try
        {
         using (ArticleFactory objFactory = new ArticleFactory())
                {
                    StringBuilder strWhere = new StringBuilder();
                    strWhere.Append("select *  from Article where 1=1 " + sbWhere + " order by dtmInTime desc,intArticleID desc");
					/*result["other"] = strWhere.ToString();
					 context.Response.Write(
           JsonMapper.ToJson(result));
		   return;*/
                    /*
                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.* from Article inner  join ArtSecRelation on Article.intArticleID = ArtSecRelation.intArticleID and Article.intState = '" + Request["type"].ToString().Trim().Replace("'","")+ "' " + " where ArtSecRelation.intSectionID=" + State.intSTID.ToString() + State.strWhereSql + " order by Article.intSort desc ,Article.dtmInTime desc,Article.intArticleID desc)t order by intSort asc, dtmInTime asc ,intArticleID asc)m " + " order by intSort asc, dtmInTime asc ,intArticleID asc)mm  order by intSort desc, dtmInTime desc ,intArticleID desc");
                    */
					
                    BusinessLogic.BusinessRules.Article objArticle = objFactory.CreatorByFullSql(strWhere.ToString());
                    if (objArticle != null)
                    {
                        List<Hashtable> reslist = new List<System.Collections.Hashtable>();
                         for (int i = 0; i < objArticle.Count; i++)
						{
							if(i==6)
							{
								break;
							}
							Hashtable temp = new Hashtable();
							temp["aid"] = objArticle.DataItem.intArticleID;
							temp["atitle"] =objArticle.DataItem.chvTitle;
							temp["atime"] =objArticle.DataItem.dtmInTime.ToString("MM-dd");
							//temp["other"] = strWhere.ToString();
							reslist.Add(temp);
							objArticle.SelectRow(i+1);
						}
						 result["list"] = reslist;
                    }
                    
                }

            context.Response.Write(
           JsonMapper.ToJson(result));
        }
        catch (Exception error)
        {
        }
    }
    /// <summary>
    /// 检测是否能转化为数字
    /// </summary>
    /// <param name="param">检测字符串</param>
    /// <returns>输出true/false</returns>
    public static bool IsNum(string param)
    {
        const string regEx = @"^[0-9]*$";
        if (Regex.IsMatch(param, regEx))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}